﻿using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsForms_Logic_Questions_Generator.Operators;

namespace WindowsForms_Logic_Questions_Generator
{
    class ExamGenXML
    {
        private XmlDocument xmlDoc;
        private XmlNode xmlNode;
        private XmlElement xmlBase, xmlElem1, xmlElem2;
        private XmlText xmlText;

        public ExamGenXML() //constructor
        {}

        public XmlDocument ConvertDataToXML(DocumentExtraData extraData, ListQestions questionsList)
        {
            xmlDoc = new XmlDocument();
            //let's add the XML declaration section
            xmlNode = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "Logic_Questions_Generator", "");
            xmlDoc.AppendChild(xmlNode);
            
            //let's add the Basic information of XML
            xmlBase = xmlDoc.CreateElement("", "LOGIC_QUESTIONS_GENERATOR", "");
            xmlText = xmlDoc.CreateTextNode("automatic exams generator - Assignemnt for Software engineering course MTA.");
            xmlBase.AppendChild(xmlText);
            xmlDoc.AppendChild(xmlBase);

            xmlElem1 = xmlDoc.CreateElement("", "EXTRA_DATA", "");
            xmlText = xmlDoc.CreateTextNode("This is extra data of each exam generated wrapping the questions.");
            xmlElem1.AppendChild(xmlText);
            xmlBase.AppendChild(xmlElem1);

            if (null != extraData)
            {
                //let's add another element (child of the EXTRA_DATA)
                xmlElem2 = xmlDoc.CreateElement("", "StudentName", "");
                xmlText = xmlDoc.CreateTextNode(extraData.GetStudentNameData());
                xmlElem2.AppendChild(xmlText);
                //xmlDoc.ChildNodes.Item(1).AppendChild(xmlElem2);
                xmlElem1.AppendChild(xmlElem2);

                //let's add another element (child of the EXTRA_DATA)
                xmlElem2 = xmlDoc.CreateElement("", "Header", "");
                xmlText = xmlDoc.CreateTextNode(extraData.GetHeaderData());
                xmlElem2.AppendChild(xmlText);
                //xmlDoc.ChildNodes.Item(1).AppendChild(xmlElem2);
                xmlElem1.AppendChild(xmlElem2);

                //let's add another element (child of the EXTRA_DATA)
                xmlElem2 = xmlDoc.CreateElement("", "Date", "");
                xmlText = xmlDoc.CreateTextNode(extraData.GetDateData());
                xmlElem2.AppendChild(xmlText);
                //xmlDoc.ChildNodes.Item(1).AppendChild(xmlElem2);
                xmlElem1.AppendChild(xmlElem2);

                //let's add another element (child of the EXTRA_DATA)
                xmlElem2 = xmlDoc.CreateElement("", "Explanation", "");
                xmlText = xmlDoc.CreateTextNode(extraData.GetExplanationData());
                xmlElem2.AppendChild(xmlText);
                //xmlDoc.ChildNodes.Item(1).AppendChild(xmlElem2);
                xmlElem1.AppendChild(xmlElem2);

                //let's add another element (child of the EXTRA_DATA)
                xmlElem2 = xmlDoc.CreateElement("", "FinalExplanation", "");
                xmlText = xmlDoc.CreateTextNode(extraData.GetFinalExplanationData());
                xmlElem2.AppendChild(xmlText);
                //xmlDoc.ChildNodes.Item(1).AppendChild(xmlElem2);
                xmlElem1.AppendChild(xmlElem2);

                //let's add another element (child of the EXTRA_DATA)
                xmlElem2 = xmlDoc.CreateElement("", "GoodLuck", "");
                xmlText = xmlDoc.CreateTextNode(extraData.GetGoodLuckData());
                xmlElem2.AppendChild(xmlText);
                //xmlDoc.ChildNodes.Item(1).AppendChild(xmlElem2);
                xmlElem1.AppendChild(xmlElem2);
            }

            xmlElem1 = xmlDoc.CreateElement("", "QUESTIONS_AND_NOTES", "");
            xmlText = xmlDoc.CreateTextNode("This is the questions and notes area.");
            xmlElem1.AppendChild(xmlText);
            xmlBase.AppendChild(xmlElem1);

            if (null != questionsList)
            {
                int nID = 0;
                foreach (ListDataInt obj in questionsList)
                {
                    //let's add element (child of the QUESTIONS_AND_NOTES)
                    xmlElem2 = xmlDoc.CreateElement("", "Question", (++nID).ToString());
                    xmlText = xmlDoc.CreateTextNode(obj.ToString());
                    xmlElem2.AppendChild(xmlText);
                    //xmlDoc.ChildNodes.Item(1).AppendChild(xmlElem2);
                    xmlElem1.AppendChild(xmlElem2);
                }
            }

            return xmlDoc;
        }

        public bool ConvertXMLToData(XmlDocument xmlDoc, DocumentExtraData extraData, ListQestions questionsList)
        {
            String str;            

            xmlNode = xmlDoc.DocumentElement;
            XmlNodeList xmlNodeList = xmlNode.ChildNodes;

            XmlNode xmlNodeExtraData = xmlNodeList.Item(1);
            foreach (XmlNode xmlNodeData in xmlNodeExtraData.ChildNodes)
            {
                str = xmlNodeData.InnerText;
                switch (xmlNodeData.Name)
                {
                    case "StudentName":
                        {
                            extraData.SetStudentNameData(str);
                        }
                        break;

                    case "Header":
                        {
                            extraData.SetHeaderData(str);
                        }
                        break;

                    case "Date":
                        {
                            extraData.SetDateData(str);
                        }
                        break;

                    case "Explanation":
                        {
                            extraData.SetExplanationData(str);
                        }
                        break;

                    case "FinalExplanation":
                        {
                            extraData.SetFinalExplanationData(str);
                        }
                        break;

                    case "GoodLuck":
                        {
                            extraData.SetGoodLuckData(str);
                        }
                        break;

                    default:
                        {
                            str += "1";
                        }
                        break;


                }
            }

            XmlNode xmlNodeQuestions = xmlNodeList.Item(2);
            foreach (XmlNode xmlNodeData in xmlNodeQuestions.ChildNodes)
            {
                switch (xmlNodeData.Name)
                {
                    case "Question":
                        {
                            ListDataInt qst = new ListDataInt(xmlNodeData.InnerText);
                            questionsList.Add(qst);
                        }
                        break;
                }
                
            }

            return true;
        }
       
    }
}
